package cn.laowen.basic.controller;


import cn.laowen.basic.dto.LoginDto;
import cn.laowen.basic.dto.WeChatDto;
import cn.laowen.basic.exception.BusinessException;
import cn.laowen.basic.service.ILoginService;
import cn.laowen.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 统一登录接口
 *
 * 1.账号密码登录接口
 * 2.1微信三方登录接口
 * 2.2绑定授权接口
 * 3.手机随机码登录接口
 * 4.邮箱随机码登录接口
 */
@RestController
@RequestMapping("/login")
public class LoginController {


    @Autowired
    private ILoginService loginServiceImpl;

    /**
     * 1.账号密码登录接口
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String, Object> map = loginServiceImpl.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 2.1微信三方扫码登录接口
     * @param weChatDto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WeChatDto weChatDto){
        try {

            return loginServiceImpl.wechat(weChatDto);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 2.2绑定授权接口
     * @param weChatDto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WeChatDto weChatDto){
        try {
            Map<String, Object> map = loginServiceImpl.binder(weChatDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
